package com.axun.ckz.yytmanager.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.axun.ckz.yytmanager.base.IBasePresenter;
import com.axun.ckz.yytmanager.base.IBaseView;
import com.axun.ckz.yytmanager.module.LoginMd;
import com.axun.ckz.yytmanager.net.PostRequest;
import com.axun.ckz.yytmanager.net.URL;
import com.axun.ckz.yytmanager.response.ConfigResponseBean;
import com.axun.ckz.yytmanager.response.LoginResponseBean;
import com.axun.ckz.yytmanager.util.SPUtils;
import com.axun.ckz.yytmanager.util.VersionUtils;
import com.axun.ckz.yytmanager.view.LoginView;
import com.example.vuandroidadsdk.okhttp.listener.DisposeDataListener;

public class LoginPresenter extends IBasePresenter {

    private Context context;
    private LoginView mView;
    private LoginMd module;
    private String url;
    private String code1;
    private String serviceName;
    public LoginPresenter(Context context, IBaseView mView) {
        super(context, mView);
        this.context = context;
        this.mView = (LoginView) mView;
        module = new LoginMd(context);
    }

    public void getUrl(final String code){
       if (code.length()>=3){
           this.code1 = code;
           PostRequest.getInstance().getConfig(code, new DisposeDataListener() {
               @Override
               public void onSuccess(Object responseObj) {
                   if (!TextUtils.isEmpty(responseObj.toString())){
                       ConfigResponseBean bean = JSON.parseObject(responseObj.toString(),ConfigResponseBean.class);
                       showService(bean.getName());
                       showVersion(VersionUtils.getVerName(context));
                       url = bean.getUrl();
                       URL.DOMAIN = bean.getUrl();
                       serviceName = bean.getName();
                   }
               }

               @Override
               public void onFailure(Object reasonObj) {
                   showFaild("网络连接失败，请检查网络");
               }
           });
       }
    }

    public void login(String user,String password,String clientId){
        if (TextUtils.isEmpty(user)){
            showError("请输入账号");
            return;
        }

        if (TextUtils.isEmpty(password)){
            showError("请输入密码");
            return;
        }
        showProgress("正在登录");
        PostRequest.getInstance().login(user, password, clientId, new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                hideDialog();
                LoginResponseBean bean = JSON.parseObject(responseObj.toString(),LoginResponseBean.class);
                if (bean.getStatus().equals("100")){
                    loginSuccess(bean);
                    module.saveUserInfo(String.valueOf(bean.getUserId()),bean.getName(),bean.getAccountId(),bean.getSessionId());
                    module.saveUrl(url);
                    URL.DOMAIN = url;
                    module.saveCode(code1);
                    SPUtils.putStringSp(context,"title",serviceName);

                }else {
                    showFaild(bean.getMessage());
                }
            }

            @Override
            public void onFailure(Object reasonObj) {
                hideDialog();
                showError("网络连接失败，请检查网络");
            }
        });
    }


    private void loginSuccess(LoginResponseBean bean){
        if (mView!=null){
            mView.loginSuccess(bean);
        }
    }


    private void loginFaild(){
        if (mView!=null){
            mView.loginFaild();
        }
    }

    private void showService(String name){
        if (mView!=null){
            mView.showService(name);
        }
    }

    private void showVersion(String version){
        if (mView!=null){
            mView.showVersion(version);
        }
    }
}
